package com.rtx.wms.std.handler.cc.finish;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.wms.std.cc.bean.ApiCCFinish;
import com.rtx.wms.std.cc.bean.RtxCctaskdetail;
import com.rtx.wms.std.cc.service.ApiCCFinishService;
import com.rtx.wms.std.cc.service.RtxCctaskdetailService;
import com.rtx.wms.std.common.bean.WmsConstant;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;

@Component
public class CCFinishHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RtxCctaskdetailService rtxCctaskdetailService;

    @Autowired
    private ApiCCFinishService apiCCFinishService;

    /**
     * 处理完成的出库单
     * @param whseid
     */
    public void handlerOrdersFinish(String whseid){
    	String batchid = IdUtil.getSnowflakeNextIdStr();
        //获取待回传的出库单
        List<RtxCctaskdetail> list = selectOrders(whseid);
        
        for(RtxCctaskdetail o : list){
            //写入回传中间表
            addApifinish(batchid,o);
            //更新状态
            updateOrders(whseid,o);
        }
    }

    /**
     * 写入回传中间表
     * @param whseid
     * @param o
     */
    private void addApifinish(String batchid, RtxCctaskdetail td) {
        try {
            ApiCCFinish apiCCFinish = convertApiOrdersfinish(td,batchid);
            
            apiCCFinishService.insert("rid_edi",apiCCFinish);
            td.setEdiflag(WmsConstant.SUCCESS_FLAG);
        } catch (Exception e) {
            logger.error("taskdetailkey-{}-写入回传中间表异常",td.getTaskdetailkey(),e);
            td.setEdiflag(WmsConstant.FAIL_FLAG);
            td.setEdimsg(e.getMessage());
        }
    }

    /**
     * 转化类型
     * @param o
     * @return
     */
    private ApiCCFinish convertApiOrdersfinish(RtxCctaskdetail c,String batchid) {
        ApiCCFinish finish = new ApiCCFinish();
        BeanUtils.copyProperties(c,finish);
        finish.setRtxqueue(0);
        finish.setEdiflag("P");
        finish.setBatchid(batchid);
        
        finish.setWhseid(c.getWhseid());
        finish.setStorerkey("GREPOW");
        finish.setRtxErptasktype(c.getRtxErptasktype());
        finish.setSku(c.getSku());
        finish.setQty(c.getQty());
        finish.setCcqty(c.getCcqty());
        finish.setDifqty(c.getDifqty());
        finish.setEditwho(c.getEditwho());
        return finish;
    }

    
    /**
     * 更新出库单
     * @param whseid
     * @param o
     */
    private void updateOrders(String whseid, RtxCctaskdetail o) {
        LambdaUpdateWrapper<RtxCctaskdetail> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(RtxCctaskdetail::getSerialkey,o.getSerialkey())
                .set(RtxCctaskdetail::getEdiflag,o.getEdiflag())
                .set(RtxCctaskdetail::getEdimsg, StrUtil.sub(o.getEdimsg(),0,490));
        rtxCctaskdetailService.updStatus(whseid,wrapper);
    }

    /**
     * 查询需要上传数据
     * @param whseid
     * @return
     */
    public List<RtxCctaskdetail> selectOrders(String whseid){
    	Calendar calendar = Calendar.getInstance();
    	calendar.add(Calendar.DAY_OF_MONTH, -7);
    	
    	LambdaQueryWrapper<RtxCctaskdetail> query = Wrappers.lambdaQuery();
    	query.eq(RtxCctaskdetail::getEdiflag, WmsConstant.DEFAULT_FLAG)
    		.ge(RtxCctaskdetail::getAdddate, new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        return rtxCctaskdetailService.queryByCondition(whseid,query);
    }
}
